package com.firstcare.android_dylibrary_demo.Main;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.firstcare.android_dylibrary.adapter.FramentPagerAdapter;
import com.firstcare.android_dylibrary_demo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Main1Activity extends FragmentActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.drawer)
    DrawerLayout drawer;
    @BindColor(R.color.main_text)
    int mainTextColor;
    @BindColor(R.color.title_background)
    int titleBackgroundColor;

    @BindViews({R.id.image1, R.id.image2, R.id.image3, R.id.image4})
    List<ImageView> imageViews;
    @BindViews({R.id.text1,R.id.text2,R.id.text3,R.id.text4})
    List<TextView> titleTextViews;

    int [] tabbarNorMalMipmaps = new int[]{R.mipmap.home_noml,R.mipmap.life_noml,R.mipmap.mall_noml,R.mipmap.me_noml};
    int [] tabbarSelectMipmaps = new int[]{R.mipmap.home_select,R.mipmap.life_select,R.mipmap.mall_select,R.mipmap.me_select};

    private Text1Fragment test1Fragment;
    private Text2Fragment test2Fragment;
    private Text3Fragment test3Fragment;
    private Text4Fragment test4Fragment;
    private List<Fragment> list;
    private FramentPagerAdapter framentAdapter;
    private String[] str={"java","android","ios","c++"};
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    public Main1Activity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        ButterKnife.bind(this);
        initData();
    }
     public void  initData(){
         ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(Main1Activity.this,android.R.layout.simple_list_item_1,str);
         listview.setAdapter(arrayAdapter);
         list = new ArrayList<Fragment>();
         fragmentManager = getSupportFragmentManager();
         fragmentTransaction = fragmentManager.beginTransaction();
         test1Fragment=new Text1Fragment();
         test2Fragment=new Text2Fragment();
         test3Fragment=new Text3Fragment();
         test4Fragment=new Text4Fragment();
         list.add(test1Fragment);
         list.add(test2Fragment);
         list.add(test3Fragment);
         list.add(test4Fragment);
         framentAdapter=new FramentPagerAdapter(fragmentManager,list);
         viewPager.setAdapter(framentAdapter);
         viewPager.setOnPageChangeListener(changeListener);
     }
    private ViewPager.SimpleOnPageChangeListener changeListener = new ViewPager.SimpleOnPageChangeListener() {
        public void onPageSelected(int position) {
            setSelectedTitle(position);
        }
    };

    private void reinitTabBar(){
        for (int i = 0; i < imageViews.size(); i++) {
            ImageView imageView = imageViews.get(i);
            imageView.setBackgroundResource(tabbarNorMalMipmaps[i]);
        }
        for (TextView textView:titleTextViews) {
            textView.setTextColor(mainTextColor);
        }
    }

    // 改变图标和文字
    private void setSelectedTitle(int position) {
        reinitTabBar();

        TextView textView = titleTextViews.get(position);
        ImageView imageView = imageViews.get(position);

        textView.setTextColor(titleBackgroundColor);
        imageView.setBackgroundResource(tabbarSelectMipmaps[position]);
    }

    @OnClick({R.id.RelativeLayout1, R.id.RelativeLayout2, R.id.RelativeLayout3, R.id.RelativeLayout4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.RelativeLayout1:
                viewPager.setCurrentItem(0);
                setSelectedTitle(0);
                break;
            case R.id.RelativeLayout2:
                viewPager.setCurrentItem(1);
                setSelectedTitle(1);
                break;
            case R.id.RelativeLayout3:
                viewPager.setCurrentItem(2);
                setSelectedTitle(2);
                break;
            case R.id.RelativeLayout4:
                viewPager.setCurrentItem(3);
                setSelectedTitle(3);
                break;
        }
    }
}
